var input = document.getElementById("upload-input");
var img = document.getElementById('upload');
input.onchange =function() {
    var file = input.files[0];
    let binaryData = [];
    binaryData.push(file);
    let imgurl=window.URL.createObjectURL(new Blob(binaryData));
    console.log(imgurl);
    img.style.backgroundImage ="url("+imgurl+")";
    img.style.backgroundSize="cover";
    img.style.border ="none";
}
let submit = document.getElementById("submit");
submit.onclick =async function () {
    let name = document.getElementById('name');
    let brief = document.getElementById('brief');
    this.disabled ="disabled";
    this.value="提交中";
    if (name.value == null || name.value === "") {
        //内容为空
        name.style.border = "0.5vw solid #DB5860";
        return;
    } else {
        name.style.border = "none";
    }
    var formdata = new FormData()
    formdata.append("file", input.files[0]);
    formdata.append('name', name.value);
    formdata.append('brief', brief.value);
    var request = new XMLHttpRequest();
    request.open("POST", "/addBook");
    request.send(formdata);
    request.onreadystatechange =state_Change;
    function state_Change()
    {
        let data =eval('(' + request.responseText + ')');
        if(data.success){
            window.location ="book.html";
        }else {
            this.disabled ="";
            this.value="创建";
            alert("出现错误，请刷新页面");
        }
    }
}


